//
//  FirstTableViewCell.m
//  MyProject
//
//  Created by apple on 15/11/10.
//  Copyright (c) 2015年 apple. All rights reserved.
//

#import "FirstTableViewCell.h"
#import "UIImageView+AFNetworking.h"


#define WIDTH [UIScreen mainScreen].bounds.size.width
@implementation FirstTableViewCell{
    NSTimer *_timer;
    NSMutableArray *_images;
}

- (void)awakeFromNib {
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}


-(void)createscrollViewWithimages:(NSArray *)images{
    
    self.arr = images;
    _images = [[NSMutableArray alloc]initWithCapacity:0];
    [_images addObject:[self.arr lastObject]];
    for (NSString *str in self.arr) {
        [_images addObject:str];
    }
    [_images addObject:[self.arr firstObject]];
    self.scrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, 160)];
    self.scrollView.pagingEnabled = YES;
    self.scrollView.delegate = self;
    [self.contentView addSubview:self.scrollView];
    self.scrollView.contentSize = CGSizeMake(WIDTH * _images.count , 0);
    self.scrollView.contentOffset = CGPointMake(WIDTH, 0);
    self.scrollView.showsHorizontalScrollIndicator = NO;
    for(int i = 0; i<_images.count; i++){
        UIImageView *imgV = [[UIImageView alloc]initWithFrame:CGRectMake(WIDTH * i, 0, WIDTH, 160)];
        [self.scrollView addSubview:imgV];
        
        [imgV setImageWithURL:[NSURL URLWithString:[_images objectAtIndex:i]] placeholderImage:nil];
    }
    [self createTimer];
    [self createPageControl];
    
    
}
- (void)createTimer{
    if (!_timer) {
        _timer = [NSTimer scheduledTimerWithTimeInterval:4 target:self selector:@selector(timeIncrease) userInfo:nil repeats:YES];
        
    }
}
- (void)createPageControl{
    if (self.arr.count <= 1) {
        
    }else{
        _pageControl = [[UIPageControl alloc] initWithFrame:CGRectMake((self.contentView.frame.size.width - 200) / 2, 160 - 31, 200, 21)];
        //_pageControl.backgroundColor = [UIColor redColor];
        _pageControl.userInteractionEnabled = NO;
        _pageControl.numberOfPages = _images.count>1?_images.count - 2:_images.count;
        
        _pageControl.currentPageIndicatorTintColor = [UIColor orangeColor];
        _pageControl.currentPage = 0;
        [self.contentView addSubview:_pageControl];

    }

}

- (void)timeIncrease{
    self.scrollView.contentOffset = CGPointMake(self.scrollView.contentOffset.x + [UIScreen mainScreen].bounds.size.width, 0);
}
#pragma mark - UIScrollViewDelegate
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    //当滚轴的偏移小于等于0时，说明已经滚到_images下标为0的元素处，那就说明用户已经看到了最后一页。
    //如果在发生右滑就要加载倒数第三页，实际内容的倒数第二页了。
    if (scrollView.contentOffset.x <= 0) {
        [scrollView setContentOffset:CGPointMake(WIDTH * (_images.count - 2), 0)];
    }
    if (scrollView.contentOffset.x >= WIDTH * (_images.count - 1)) {
        [scrollView setContentOffset:CGPointMake(WIDTH, 0)];
    }
    [self scrollViewDidEndDecelerating:self.scrollView];
}

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
    int page = (self.scrollView.contentOffset.x ) / [UIScreen mainScreen].bounds.size.width;
    if ([_images count] > 1)
    {
        page --;
        if (page >= _pageControl.numberOfPages)
        {
            page = 0;
        }else if(page <0)
        {
            page = (int)_pageControl.numberOfPages - 1;
        }
    }
    _pageControl.currentPage = page;
}




@end
